TARGET = 'bin/twitterExample'
SOURCES  = ['main.cpp', 'testApp.cpp']
DEBUG = 1
ADDONS = []

LIBS = []
LIBPATH = []
INCLUDES = []

# env
env = Environment()

platform = env['PLATFORM']

if platform == 'darwin':
	PLATFORM_LIB_PATH = '/lib/osx/'
elif platform == 'posix':
	PLATFORM_LIB_PATH = '/lib/linux/'

# twitter 
TWITTER_SRC = ['Stream.cpp', 'TwitPic.cpp', 'Twitter.cpp', \
		'parser/JSON.cpp', 'types/Tweet.cpp', 'types/URL.cpp']
TWITTER_SRC = ['src/Twitter/' + f for f in TWITTER_SRC]
TWITTER_INCLUDES = ['libs/jansson/include/osx', \
		'src/Twitter', 'src/Twitter/parser',
		'src/Twitter/types']
TWITTER_LIBS = [File('libs/curl' + PLATFORM_LIB_PATH + 'libcurl.a'), \
				File('libs/curl' + PLATFORM_LIB_PATH + 'libssl.a'), \
				File('libs/curl' + PLATFORM_LIB_PATH + 'libiconv.a'), \
				File('libs/curl' + PLATFORM_LIB_PATH + 'libidn.a'), \
				File('libs/curl' + PLATFORM_LIB_PATH + 'libintl.a'), \
				File('libs/curl' + PLATFORM_LIB_PATH + 'libz.a'), \
		File('libs/jansson' + PLATFORM_LIB_PATH + 'libjansson.a')]

CURL_SRC = ['Curl.cpp', 'FTP.cpp', 'Request.cpp']
CURL_SRC = ['src/Curl/' + f for f in CURL_SRC]
CURL_OAUTH_SRC = ['Header.cpp', 'Signature.cpp', 'Utils.cpp', 'oAuth.cpp']
CURL_OAUTH_SRC = ['src/Curl/oauth/' + f for f in CURL_OAUTH_SRC]
CURL_PARAMETER_SRC = ['Collection.cpp', 'File.cpp', 'Parameter.cpp']
CURL_PARAMETER_SRC = ['src/Curl/parameter/' + f for f in CURL_PARAMETER_SRC]
CURL_SRC += CURL_OAUTH_SRC + CURL_PARAMETER_SRC
CURL_INCLUDES = ['libs/curl/include/osx', \
		'src/Curl', 'src/Curl/parameters', 'src/Curl/oauth']

CRYPTO_SRC = ['HMAC_SHA1.cpp', 'SHA1.cpp', 'base64.cpp', 'urlencode.cpp']
CRYPTO_SRC = ['libs/crypto/src/' + f for f in CRYPTO_SRC]
CRYPTO_INCLUDES = ['libs/crypto/include']
CRYPTO_LIBS = [File('libs/crypto' + PLATFORM_LIB_PATH + 'libcrypto.a')]

SOURCES = ['#src/' + s for s in SOURCES]
SOURCES += TWITTER_SRC + CURL_SRC + CRYPTO_SRC
INCLUDES += TWITTER_INCLUDES + CURL_INCLUDES + CRYPTO_INCLUDES
LIBS += TWITTER_LIBS + CRYPTO_LIBS

FTGL_INCLUDES = ['libs/ftgl/include']
FTGL_LIBS = [File('libs/ftgl' + PLATFORM_LIB_PATH + 'libftgl.a')]
INCLUDES += FTGL_INCLUDES
LIBS += FTGL_LIBS

CCFLAGS = '-Wreturn-type -Wunused-variable -ffast-math '

OF_PATH = '../../../libs/'
OF_LIB_PATH = 'openFrameworksCompiled/'


if DEBUG:
	STATIC_LIBS = [(OF_LIB_PATH, 'openFrameworksDebug.a')]
else:
	STATIC_LIBS = [(OF_LIB_PATH, 'openFrameworks.a')]

CPPPATH = ['/usr/include',
		OF_PATH + 'openFrameworks',
		OF_PATH + 'cairo/include/cairo',
		OF_PATH + 'freetype2/freetype',
		OF_PATH + 'freetype/include/freetype2']

CPPPATH += INCLUDES

for d in Split('3d app communication events gl graphics math sound types utils video'):
	CPPPATH += [OF_PATH + 'openFrameworks/' + d]

OF_LIB_DIRS = Split('FreeImage assimp glew freetype rtAudio tess2')
if platform == 'posix':
	OF_LIB_DIRS += ['kiss']
elif platform == 'darwin':
	OF_LIB_DIRS += ['poco']

for d in OF_LIB_DIRS:
	CPPPATH += [OF_PATH + d + '/include']
	LIBPATH += [OF_PATH + d + PLATFORM_LIB_PATH]

STATIC_LIBS += [('tess2', 'tess2.a'),
			('assimp', 'assimp.a')]

DYNAMIC_LIBS = []

if platform == 'darwin':
	LIBPREFIX = ''
	STATIC_LIBS += [('FreeImage', 'freeimage.a'),
			('freetype', 'freetype.a'),
			('poco', ('PocoFoundation.a', 'PocoNet.a', 'PocoUtil.a', 'PocoXML.a')),
			('glew', 'glew.a')]
	#LIBPATH += ['/opt/local/lib']

	# for leopard
	CCFLAGS += ' -arch i386 '
	CCFLAGS += ' -mmacosx-version-min=10.5 '
	CCFLAGS += ' -DMAC_OS_X_VERSION_MAX_ALLOWED=MAC_OS_X_VERSION_10_5 ' + \
		'-DMAC_OS_X_VERSION_MIN_REQUIRED=MAC_OS_X_VERSION_10_5 '
	STATIC_LIBS += [('cairo', Split('cairo-script-interpreter.a cairo.a pixman-1.a'))]

elif platform == 'posix':
	LIBPREFIX = 'lib'
	CPPPATH += ['/usr/include/gstreamer-0.10',
			'/usr/include/glib-2.0',
			'/usr/lib/glib-2.0/include',
			'/usr/include/libxml2']
	STATIC_LIBS += [('kiss', 'kiss.a')]
	DYNAMIC_LIBS += ['freeimage', 'freetype', 'GLEW', 'GL', 'GLU', 'glut', 'pthread',
			'cairo', 'openal', 'sndfile', 'PocoFoundation', 'PocoNet', 'PocoUtil', 'PocoXML',
			'z',
			# gstreamer libs
			'gstreamer-0.10', 'gobject-2.0', 'gmodule-2.0', 'xml2',
			'gstapp-0.10', 'gstbase-0.10', 'gstvideo-0.10',
			'gthread-2.0', 'rt', 'glib-2.0']

for (dir, libs) in STATIC_LIBS:
	if type('str') == type(libs):
		LIBS += [File(OF_PATH + dir + PLATFORM_LIB_PATH + LIBPREFIX + libs)]
	else:
		LIBS += [File(OF_PATH + dir + PLATFORM_LIB_PATH + LIBPREFIX + l) for l in libs]


LIBS += DYNAMIC_LIBS

# dynamic libs for 10.5, static libs are compiled for 10.6
if platform == 'darwin':
	# FIXME: add 10.5 static libs for poco
	#LIBS += ['PocoFoundation', 'PocoNet']
	pass

if DEBUG:
	CCFLAGS += '-ggdb2 -O0 -DDEBUG=1 '
else:
	CCFLAGS += '-g0 -O3 '

# remove fmod
CCFLAGS += '-DOF_NOSOUND '

# process addons
if 'ADDONS' not in locals():
	ADDONS = []

if 'ofxAssimpModelLoader' in ADDONS:
	SOURCES += ['../../../../addons/ofxAssimpModelLoader/src/ofxAssimpModelLoader.cpp']
	CPPPATH += ['../../../../addons/ofxAssimpModelLoader/src',
			'../../../../addons/ofxAssimpModelLoader/libs/assimp/include']

if 'ofxSimpleGuiToo' in ADDONS:
	ADDONS += ['ofxXmlSettings']
	OFXMSAIOLOCATION = '../../../../addons/ofxMSAInteractiveObject/src/'
	OFXMSAIOLIB = [OFXMSAIOLOCATION + 'ofxMSAInteractiveObject.cpp']

	OFXSIMPLEGUITOOLOCATION = '../../../../addons/ofxSimpleGuiToo/src/'
	OFXSIMPLEGUITOOLIB = ['ofxSimpleGuiConfig.cpp', 'ofxSimpleGuiControl.cpp',
		'ofxSimpleGuiPage.cpp', 'ofxSimpleGuiToo.cpp', 'Controls/ofxSimpleGuiButton.cpp',
		'Controls/ofxSimpleGuiColorPicker.cpp', 'Controls/ofxSimpleGuiContent.cpp',
		'Controls/ofxSimpleGuiComboBox.cpp',
		'Controls/ofxSimpleGuiFPSCounter.cpp', 'Controls/ofxSimpleGuiMovieSlider.cpp',
		'Controls/ofxSimpleGuiQuadWarp.cpp', 'Controls/ofxSimpleGuiSlider2d.cpp',
		'Controls/ofxSimpleGuiTitle.cpp', 'Controls/ofxSimpleGuiToggle.cpp']
	OFXSIMPLEGUITOOLIB = [OFXSIMPLEGUITOOLOCATION + s for s in OFXSIMPLEGUITOOLIB]
	SOURCES += OFXMSAIOLIB + OFXSIMPLEGUITOOLIB
	CPPPATH += [OFXMSAIOLOCATION, OFXSIMPLEGUITOOLOCATION,
			OFXSIMPLEGUITOOLOCATION + 'Controls']

if 'ofxXmlSettings' in ADDONS:
	OFXXMLSETTINGSLOCATION = '../../../../addons/ofxXmlSettings/'
	OFXXMLSETTINGSLIB = ['libs/tinyxml.cpp', 'libs/tinyxmlerror.cpp',
		'libs/tinyxmlparser.cpp', 'src/ofxXmlSettings.cpp']
	OFXXMLSETTINGSLIB = [OFXXMLSETTINGSLOCATION + s for s in OFXXMLSETTINGSLIB]
	SOURCES += OFXXMLSETTINGSLIB
	CPPPATH += [OFXXMLSETTINGSLOCATION + 'src', OFXXMLSETTINGSLOCATION + 'libs']

if 'ofxOpenNI' in ADDONS:
	OFXOPENNILOCATION = '../../../../addons/ofxOpenNI/'
	OFXOPENNILIB = Split('ofxDepthGenerator.cpp ofxGestureGenerator.cpp ' + \
		'ofxHandGenerator.cpp ofxHardwareDriver.cpp ofxIRGenerator.cpp ' + \
		'ofxImageGenerator.cpp ' + \
		'ofxOpenNIContext.cpp ofxOpenNIRecorder.cpp ' + \
		'ofxTrackedHand.cpp ofxTrackedUser.cpp ofxUserGenerator.cpp')
	SOURCES += [OFXOPENNILOCATION + 'src/' + s for s in OFXOPENNILIB]
	CPPPATH += [OFXOPENNILOCATION + s for s in ['include', 'include/openni',
		'include/libusb', 'include/nite', 'src']]
	LIBS += ['OpenNI', 'usb-1.0']

LINKFLAGS = ''

if platform == 'darwin':
	LINKFLAGS += ' -dead_strip -arch i386 '

env.Append(CCFLAGS = CCFLAGS)
env.Append(CPPPATH = CPPPATH)
env.Append(LIBPATH = LIBPATH)
env.Append(LIBS = LIBS)
env.Append(LINKFLAGS = LINKFLAGS)

if platform == 'darwin':
	env.Append(FRAMEWORKS = Split('ApplicationServices QuickTime Carbon OpenGl GLUT ' + \
			'CoreServices '))
	env.Append(FRAMEWORKPATH = '/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/')

# build the application
env.Program(source = SOURCES, target = TARGET, variant_dir = 'bin', duplicate = 0)

# build the osx app
'''
if platform == 'darwin':
	import sys, os
	from osxbundle import *
	TOOL_BUNDLE(env)

	if 'RESOURCES' not in locals():
		RESOURCES = []
	else:
		RESOURCES = ['#../resources/' + s for s in RESOURCES]

	if 'PLIST' not in locals():
		PLIST = '#../xcode/Info.plist',
	else:
		PLIST = '#' + PLIST
		
	bundle = env.Alias('app', env.MakeBundle(TARGET + '.app',
		TARGET,
		'key',
		PLIST,
		resources = RESOURCES,
		typecode='APPL',
		icon_file = '../tools/TinderBox.app/Contents/Resources/CinderApp.icns',
		subst_dict = {'\${EXECUTABLE_NAME}': TARGET[1:],
			          '\${PRODUCT_NAME}': TARGET[1:]}))

	env.Clean(TARGET, TARGET + '.app')
'''

# run
#env.AlwaysBuild(env.Alias('run', [TARGET + '.app'], 'open %s' % env.Dir(TARGET + '.app').abspath))
from os.path import dirname, basename
from os import environ

if platform == 'posix':
	env['ENV']['DISPLAY'] = environ['DISPLAY']
	if 'LIBGL_DRIVERS_PATH' in environ:
		env['ENV']['LIBGL_DRIVERS_PATH'] = environ['LIBGL_DRIVERS_PATH']

env.AlwaysBuild(env.Alias('run', TARGET, 'cd %s; ./%s' % (dirname(env.File(TARGET).abspath), basename(TARGET))))

